using System.Xml.Serialization;
using NUnit.Framework;
using ValidationFramework.Configuration;
using ValidationFramework.Extensions;

namespace ValidationFramework.Tests.Configuration
{
    [TestFixture]
    public class RequiredObjectRuleConfigReaderTest
    {
        [XmlRoot("person")]
        public class Person
        {
            [XmlAttribute("name")]
            public string Name
            {
                get;
                set;
            }

            [XmlAttribute("age")]
            public int Age
            {
                get;
                set;
            }

        }
        #region Methods

        [Test]
        public void ReadConfigComplex()
        {
            const string validatorXml = "<rule errorMessage='hello' typeName='RequiredObjectRule' ruleSet='foo' ><person name='john' age='23'/></rule>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Person>)ConfigurationService.GetRule(ruleData, typeof(Person).TypeHandle);
            Assert.AreEqual("hello", rule.ErrorMessage);
            var person = rule.InitialValue;

            Assert.AreEqual(23, person.Age);
            Assert.AreEqual("john", person.Name);

        }


        [Test]
        public void ReadConfigSimple()
        {
            const string validatorXml = "<rule typeName='RequiredObjectRule'/>";
            var ruleData = (RuleData.Read(validatorXml.ToXmlReader()));
            var rule = (RequiredRule<Person>)ConfigurationService.GetRule(ruleData, typeof(Person).TypeHandle);
            Assert.IsNull(rule.ErrorMessage);
            Assert.IsFalse(rule.HasInitialValue);
        }
        #endregion


    }
}